//
//  ZLPublicNoticeTableViewController.m
//  ZLExamination
//
//  Created by Lei_Zhong_Xun on 2017/8/21.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLPublicNoticeTableViewController.h"
#import "ZLPublicNoticeDetailViewController.h"
#import "ZLPublicNoticeTableViewCell.h"
#import "ZLPublicNoticeModel.h"

@interface ZLPublicNoticeTableViewController ()
/** 列表数组*/
@property (nonatomic,strong) NSMutableArray* listDataArray;

@end

@implementation ZLPublicNoticeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"公告通知";
    [self.myTableView registerClass:[ZLPublicNoticeTableViewCell class] forCellReuseIdentifier:ZLPublicNoticeTableViewCellID];
    self.view.backgroundColor = [UIColor whiteColor];
    [self listLoadData];
}

#pragma mark - 数据请求
- (void)listLoadData
{
    _listDataArray = [NSMutableArray array];
    [[YCRequestDataService sharedManager] Post:@"notice/notice/lists" parameters:nil success:^(id response) {
        if (response[@"error_code"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            NSArray<NSDictionary *> *lists = response[@"response_data"][@"lists"];
            [lists enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZLPublicNoticeModel *noticeModel = [[ZLPublicNoticeModel alloc] initWithDataDic:obj];
                [_listDataArray addObject:noticeModel];
            }];
        }
        [self.myTableView reloadData];
    }];
}

//转换时间
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZLPublicNoticeTableViewCell *contentCell = [tableView dequeueReusableCellWithIdentifier:ZLPublicNoticeTableViewCellID forIndexPath:indexPath];
     if (_listDataArray.count != 0) {
         ZLPublicNoticeModel *noticeModel = _listDataArray[indexPath.row];
         contentCell.titleLabel.text = noticeModel.title;
         NSString *timeStr = [self timeWithTimeIntervalString:noticeModel.add_time];
         contentCell.timeLabel.text = timeStr;
         
         contentCell.contentLabel.hidden = YES;
         
//         NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[noticeModel.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
//         
//         contentCell.contentLabel.attributedText = attrStr;
//         
     }
    
    return contentCell;
}


#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}
//tableview左面线不能顶到头的问题。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZLPublicNoticeDetailViewController *detailVC = [[ZLPublicNoticeDetailViewController alloc] init];
    if (_listDataArray.count != 0) {
        ZLPublicNoticeModel *noticeModel = _listDataArray[indexPath.row];
        detailVC.n_id = noticeModel.n_id;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
